package com.audaque.springboot.foshanupload.web.streamdemo.annoMode.provider.impl;


import com.audaque.springboot.foshanupload.web.streamdemo.annoMode.channel.TestChannel;
import com.audaque.springboot.foshanupload.web.streamdemo.annoMode.provider.IMessageProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.binding.BinderAwareChannelResolver;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;


@Component
public class MessageProviderImpl implements IMessageProvider {

  // 消息的发送管道
  @Resource
  private MessageChannel output;
  @Resource
  private TestChannel testChannel;

  @Autowired
  private BinderAwareChannelResolver channelResolver;


  @Override
  public void send(String msg) {
    Message<String> message = MessageBuilder.withPayload(msg).build();
    // 创建并发送消息
    //方式1
    this.output.send(message);
    //方式2
    testChannel.testOutput().send(message);
  }


  @Override
  public void send(String msg, String channel) {
    Map<String, Object> payload = new HashMap<>();
    payload.put("msg", msg);
    Message<Map<String, Object>> build = MessageBuilder.withPayload(payload).build();
    channelResolver.resolveDestination(channel).send(build);
  }


}
